//拼团订单列表数据,微信支付签名,取消订单，确定收货
import { groupsGetList, setPlayOride, getDeleOreder, getOrderFinis, setUtilTask} from '../../config/https';  
import Toast from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/toast/toast';
import Dialog from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/dialog/dialog';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],                      //订单列表数据
    status: 0,                    // 0 全部 1待付款  2待成团 3已成团 4未成团
    dispatchtype:0,               // 0配送 1 自提
    page:0,                       //分页
    loadMore: false,              //加载中提示
    finished: false,              //是否已经请求完全部数据
    navIndex: 1,                  //配送、自提字体激活状态
    active:0,                     //tab字体激活状态       
    peiSon:'',                    //配送跳转地址
    ziTi: '',                     //自提跳转地址
    id:''                         //当前点击列表的id
  },

  onImageError(e){ //监听图片404状态
    let index = e.currentTarget.dataset.index;
    let list = this.data.list;
    list[index].thumb ='../../img/noimg.png';
    this.setData({list})
  },

  // -----------------------------------------------------------------
  //点击 配送订单 或者 自提订单
  navClick(e) {
    this.onInit();
    let id = e.currentTarget.dataset.id;        //1配送2自提
    this.setData({ navIndex: id, dispatchtype: Number(id) - 1, active: 0, status:0 }) //颜色激活
    this.groupsList()
  },
  // 点击订单状态 ，未付款，成团等等
  onChange(e) { 
    this.onInit()
    let index = e.detail.index; //当前点击下标
    console.log(index)
    this.setData({ active: index, status:index==0?index:index+1})
    this.groupsList()  //拼团订单列表数据
  },
// ------------------------------------------------------------------------------
  navget() { //订单列表跳转
    if (this.data.navIndex == 1) { //配送
      wx.navigateTo({
        url:this.data.peiSon + this.data.id, 
      })
    } else if (this.data.navIndex == 2) { //自提
      wx.navigateTo({
        url: this.data.ziTi + this.data.id,
      })
    }
    
  },
  //订单详情
  details(e){ 
    
    let that = this
    let statusstr=e.currentTarget.dataset.statusstr; //状态
    let { orderid, teamid} = e.currentTarget.dataset; //点击时此项订单号
    let dispatchtype = that.data.dispatchtype; // 0配送 1 自提
    if (this.data.dispatchtype==0){
      wx.navigateTo({
        url: '/pages/detailpss/detailpss?orderid=' + orderid + '&teamid=' + teamid,
      })
    }else{
      
      wx.navigateTo({
        url: '/pages/detailzt/detailzt?orderid=' + orderid + '&teamid=' + teamid,
      })
    }
  
  },
// ------------------------------点击按钮------------------------------------------------
  //确定收货配送订单
  onClose(e){

    let that = this
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    let list = that.data.list
    Dialog.confirm({
      message: '是否确认收货？',
    }).then(() => { //确定的时候跳转到 订单详情
      getOrderFinis({
        id: id
      }).then(res => {
        list[index].statusstr = "已完成"
        list[index].status = 3
        list[index].success = 1
        that.setData({
          list
        })
        Toast('收货成功~~')
      }).catch(err => {
        Toast(err.message)
      })
    })
  },
  //售后
  afterSales(e){
    let { orderid, teamid, index} = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/buyingrefund/buyingrefund?orderid=' + orderid + '&teamid=' + teamid +'&index='+index,
    })
  },
  //去支付
  toPayFor(e){
    let that = this;
    wx.showLoading({
      title: '请稍后',
    })
    let list = that.data.list
    let { index, id, teamid} = e.currentTarget.dataset
    // 微信支付签名数据
    setPlayOride({ 
      orderid: id,
      teamid: teamid
    }).then(res => {
      var payInfo = res.wechat
      wx.requestPayment({ //微信支付
        'timeStamp': payInfo.timeStamp, //时间戳，
        'nonceStr': payInfo.nonceStr,  //随机字符串
        'package': payInfo.package, //统一下单接口返回的 prepay_id 参数值
        'signType': payInfo.signType,  //签名算法
        'paySign': payInfo.paySign,  //签名
        'success': res => {
          if(that.setData.status==0){
            list[index].statusstr='已付款'
          }else if(that.data.status==1){
            list.splice(index,1)
          }
          that.setData({
            list
          })
          wx.hideLoading()
          wx.navigateTo({
            url: '/pages/paymenstapur/paymenstapur?type=0&orderid=' + id + '&teamid=' + teamid,
          })
          // that.groupsList()  //调用订单列表接口
        },
        'fail': err => {
          wx.hideLoading()
          wx.navigateTo({
            url: '/pages/paymensta/paymensta?type=1',
          })
        }
      })
    })
  },



  //数据清空
  onInit(){
    this.setData({
      list:[],
      page:0
    })
  },



// ------------------------------------------------------------------------------
groupsList() { //拼团订单列表数据
  let that = this
  that.setData({
    loadMore: true
  })
  wx.showLoading({
    title: '加载中',
  })
  groupsGetList({
    page:++that.data.page,
    dispatchtype: that.data.dispatchtype, // 0配送 1 自提
    status: that.data.status //0 全部 1待付款  2待成团 3已成团 4未成团
  }).then(res=>{
    console.log(res)
    wx.hideLoading()
    let list = res.list
    list = that.data.list.concat(list)
    that.setData({
      loadMore: false
    })
    if (list.length == 0) {
    } else {
      that.setData({
        finished: res.list.length < res.pagesize ? true : false
      })
    }
    wx.stopPullDownRefresh();
    that.setData({
      list
    })
    // setUtilTask({})
  })
},
 
  //取消订单
  onDle(e){
    let that = this
    let { id, index } = e.currentTarget.dataset
    let list= that.data.list
    Dialog.confirm({
      message: '确定取消订单吗？',
    }).then(() => { 
      getDeleOreder({ id:id }).then(res => {
        if(that.data.status==0){
          list[index].statusstr = '已取消';
        }else{
          list.splice(index,1)
        }
        that.setData({
          list
        })
      }).catch(err => {
        Toast("取消失败~~")
      })
    })
    
  },
  onLoad: function (options) { //生命周期函数--监听页面加载
    
  },

 
  onReady: function () { //生命周期函数--监听页面初次渲染完成

  },


  onShow: function () { //生命周期函数--监听页面显示
    this.onInit()
    this.groupsList() //拼团订单列表数据
  },


  onHide: function () { //生命周期函数--监听页面隐藏

  },

  onUnload: function () { //生命周期函数--监听页面卸载

  },


  onPullDownRefresh: function () { //页面相关事件处理函数--监听用户下拉动作

  },

 
  onReachBottom: function () { //页面上拉触底事件的处理函数
    if (!this.data.finished && !this.data.loadMore) {
      this.groupsList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})